package admin

import (
	"exercise/application/yiran/app/libs"
	"exercise/application/yiran/app/request/adminRequest"
	"exercise/application/yiran/app/service/admin"
	"github.com/gin-gonic/gin"
)

type studentScoreController struct {
}

// InitStudent 初始化学生
func (*studentScoreController) InitStudent(ctx *gin.Context) {
	request := new(adminRequest.InitStudentRequest)
	err := libs.BindRequest(ctx, request)
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}
	err = admin.NewStudentScoreService(ctx).InitStudent(request)
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}

	libs.OkToast(ctx, "写入成功")
}

func (*studentScoreController) ClearStudent(ctx *gin.Context) {
	err := admin.NewStudentScoreService(ctx).ClearStudent()
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}

	libs.OkToast(ctx, "清除成功")
}

// ImportStudentScore 导入成绩
func (*studentScoreController) ImportStudentScore(ctx *gin.Context) {
	request := new(adminRequest.ImportStudentScoreRequest)
	err := libs.BindRequest(ctx, request)
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}
	ret, err := admin.NewStudentScoreService(ctx).ImportStudentScore(request)
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}

	//libs.OkToast(ctx, "写入成功")
	libs.Success(ctx, ret)
}

// UpdateRanking 更新排名
func (*studentScoreController) UpdateRanking(ctx *gin.Context) {
	total, err := admin.NewStudentScoreService(ctx).UpdateRanking()
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}

	libs.Success(ctx, total)
}

// LevelGrading 等级赋分
func (*studentScoreController) LevelGrading(ctx *gin.Context) {
	total, err := admin.NewStudentScoreService(ctx).LevelGrading()
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}

	libs.Success(ctx, total)
}

// Grading 分数转成等级
func (*studentScoreController) Grading(ctx *gin.Context) {
	total, err := admin.NewStudentScoreService(ctx).Grading()
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}

	libs.Success(ctx, total)
}

// CalScore 计算总得分
func (*studentScoreController) CalScore(ctx *gin.Context) {
	total, err := admin.NewStudentScoreService(ctx).CalScore()
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}

	libs.Success(ctx, total)
}

// Assignment 赋分
func (*studentScoreController) Assignment(ctx *gin.Context) {
	err := admin.NewStudentScoreService(ctx).Assignment()
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}

	libs.OkToast(ctx, "成功")
}

func NewStudentScoreController() *studentScoreController {
	return &studentScoreController{}
}
